﻿package chenji.moreitemfunapi.item;

import java.util.ArrayList;
import java.util.List;

import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;

import com.google.common.collect.Lists;

import chenji.itemeditor.nbt.base.NBTTagCompound;
import chenji.itemeditor.util.NMSItem;
import chenji.moreitemfunapi.eventprocessor.EventProcessor;
import chenji.moreitemfunapi.guide.GuideProcessor;
import chenji.moreitemfunapi.textures.info.TextureInfo;

public class MifItem {
	/* 识别名 */
	private String identifyname;
	/* 物品样式 */
	private ItemStack item;
	/* 是否开启材质 */
	private boolean enabletextures;
	/* 物品别名（用于检索） */
	private ArrayList<String> othername;
	/* 事件处理器 */
	private EventProcessor ep;
	/* 向导书处理器 */
	private GuideProcessor gp;
	/* 加载这个物品的插件名称 */
	private String pluginName;

	/* 缓存物品 */
	private ItemStack cacheitem;

	private TextureInfo info;

	public MifItem(String identifyname, ItemStack item) {
		this.identifyname = identifyname;
		this.item = item;
		this.othername = Lists.newArrayList();
		this.spawnItem();
	}

	public MifItem(String identifyname, ItemStack item, boolean enabletextures) {
		this(identifyname, item);
		this.enabletextures = enabletextures;
		this.spawnItem();
	}

	public MifItem(String identifyname, ItemStack item, boolean enabletextures, ArrayList<String> othername) {
		this(identifyname, item, enabletextures);
		this.othername = othername;
		this.spawnItem();
	}

	public MifItem(String identifyname, ItemStack item, boolean enabletextures, ArrayList<String> othername, EventProcessor ep, GuideProcessor gp) {
		this(identifyname, item, enabletextures, othername);
		this.ep = ep;
		this.gp = gp;
	}

	/**
	 * 设定是否开启材质
	 * 
	 * @param arg
	 *            是否开启
	 * @return 自身
	 */
	public final MifItem setEnableTextures(boolean arg) {
		this.enabletextures = arg;
		this.spawnItem();
		return this;
	}

	/**
	 * 设置事件处理器
	 * 
	 * @param ep
	 *            事件处理器
	 * @return 自身
	 */
	public final MifItem setEventProcessor(EventProcessor ep) {
		this.ep = ep;
		return this;
	}

	/**
	 * 添加一个别名
	 * 
	 * @param othername
	 *            别名
	 * @return 自身
	 */
	public final MifItem addOthername(String othername) {
		this.othername.add(othername);
		return this;
	}

	/**
	 * 添加一堆别名
	 * 
	 * @param names
	 *            别名
	 * @return 自身
	 */
	public final MifItem addOtherNames(List<String> names) {
		this.othername.addAll(names);
		return this;
	}

	/**
	 * 获取缓存中的物品
	 * 
	 * @return 物品
	 */
	public final ItemStack getItem() {
		return this.cacheitem;
	}

	public final ItemStack getModelItem() {
		return this.item;
	}

	public final GuideProcessor getGuideProcessor() {
		return this.gp;
	}

	public final MifItem setGuideProcessor(GuideProcessor gp) {
		this.gp = gp;
		return this;
	}

	private ItemStack spawnItem() {
		NMSItem nmsitem = NMSItem.getItem(item);
		NBTTagCompound nbt = nmsitem.getTag();
		if (nbt.getString("mif.texturesname") != null) {
			nbt.remove("mif.texturesname");
		}
		nbt.setString("mif.identifyname", this.identifyname);
		if (this.enabletextures && this.identifyname != null) {
			nbt.setString("miftexturesname", this.identifyname.replaceAll("_", ""));
		}
		nmsitem.setTag(nbt);
		this.cacheitem = nmsitem.getBukkitItem();
		return this.cacheitem;

	}

	public final String getIdentifyname() {
		return identifyname;
	}

	public final boolean isEnabletextures() {
		return enabletextures;
	}

	public final String getTexturesname() {
		return identifyname;
	}

	public final ArrayList<String> getOthername() {
		return othername;
	}

	public final String getPluginName() {
		return this.pluginName;
	}

	public final void setPluginName(String pluginname) {
		this.pluginName = pluginname;
	}

	public final EventProcessor getEventProcessor() {
		return ep;
	}

	public final void setOthername(ArrayList<String> othername) {
		this.othername = othername;
	}

	public final Material getType() {
		return this.item.getType();
	}

	public void setTextureInfo(TextureInfo info) {
		this.info = info;
	}

	public TextureInfo getTextureInfo() {
		return info;
	}

}
